import router from "@/router"
import Cookie from "js-cookie"

const tab = {
    state:{
        isCollapse:false,
        tabsList:[
            {
                path:'/',
                name:'home',
                label:'首页',
                icon:'s-home',
                url:'Home/Home'
            },
        ],
        menu:[]
    },
    actions:{},
    mutations:{
        collapseMenu(state){
            state.isCollapse = !state.isCollapse
        },
        SELECTION_MENU(state,value){
            if(value.name !== 'home'){
                const index = state.tabsList.findIndex(item => item.name ===value.name)
                if(index === -1){
                    state.tabsList.push(value)
                }
            }
        },
        CLOSE_TAGS(state,value){
            // console.log(value);
            const index = state.tabsList.findIndex(val => val.name === value.name)
            state.tabsList.splice(index,1)
        },

        SET_MENU(state,value){
            state.menu = value
            Cookie.set('menu',JSON.stringify(value)) //这里设置vuex的数据不缓存
        },
        //动态注册路由
        ADD_MENU(state,router){
            if(!Cookie.get('menu')) return
            const menu = JSON.parse(Cookie.get('menu'))
            state.menu = menu
            const menuArr = []
            menu.forEach(item => {
                if(item.children){
                    item.children = item.children.map((item)=>{
                        item.component = () => import(`../views/${item.url}`)
                        return item
                    })
                    menuArr.push(...item.children)
                }else{
                    item.component = () => import(`../views/${item.url}`)
                    menuArr.push(item)
                }
            });
            menuArr.forEach((item)=>{
                router.addRoute('main',item)
            })
        }
    },
    
}

export default tab